package com.a3dweijing.wetouch2.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.base.BaseActivity;
import com.a3dweijing.wetouch2.bean.LoginMsg;
import com.a3dweijing.wetouch2.datainterface.Getdataimpl;
import com.a3dweijing.wetouch2.datasave.Singleton;
import com.a3dweijing.wetouch2.tools.SharedPreUtil;
import com.google.gson.Gson;

import java.util.Timer;
import java.util.TimerTask;

public class WelcomeActivity extends BaseActivity {
    private Handler handler;
    private boolean flag=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                endPopupwindow();
                flag=false;
                switch (msg.what){
                    case 100:
                        LoginMsg loginMsg=new Gson().fromJson(msg.obj.toString(),LoginMsg.class);
                        if (loginMsg.isFlag())
                        {
                            Singleton.getInstance().setUser(loginMsg.getUser());
                            Intent intent=new Intent(WelcomeActivity.this,MainActivity.class);
                            startActivity(intent);
                            finish();
                        }else {
                            startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
                            finish();
                        }
                        break;
                    case 404:
                        startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
                        finish();
                        break;
                }
            }
        };
    }

    @Override
    protected void onStart() {
        super.onStart();
        String username = SharedPreUtil.getString(getBaseContext(), "username",null);
        String userpwd = SharedPreUtil.getString(getBaseContext(), "userpwd",null);
        if (username != null && userpwd != null) {
            flag=true;
            new LoginThread(username,userpwd).start();
        } else {
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
                    finish();
                }
            }, 3000);
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(flag)
        {
            startPopupwindow(getBaseContext(),"自动登录中");
        }
    }

    private class LoginThread extends Thread
    {
        String phone_number;
        String password;

        public LoginThread(String phone_number, String password) {
            this.phone_number = phone_number;
            this.password = password;
        }

        @Override
        public void run() {
            super.run();
            String resulet=new Getdataimpl().remotUserLogin(phone_number,password);
            Log.e("--",resulet);
            if (resulet!=null && resulet.length()>7)
            {
                handler.sendMessage(handler.obtainMessage(100,resulet));
            }else {
                handler.sendMessage(handler.obtainMessage(404, "网络繁忙，请稍后再试"));
            }
        }
    }
}
